1
|
|
|
/** |
2
|
|
|
upgrades |
3
|
|
|
Component that handles upgrades. |
4
|
|
|
|
5
|
|
|
@namespace Components |
6
|
|
|
*/ |
7
|
|
|
'use strict'; |
8
|
|
|
|
9
|
|
|
angular.module('game').component('upgrades', { |
10
|
|
|
templateUrl: 'views/upgrades.html', |
11
|
|
|
controller: ['state', 'visibility', 'upgrade', 'data', upgrades], |
12
|
|
|
controllerAs: 'ct' |
13
|
|
|
}); |
14
|
|
|
|
15
|
|
|
function upgrades(state, visibility, upgradeService, data) { |
16
|
|
|
let ct = this; |
17
|
|
|
ct.state = state; |
18
|
|
|
ct.data = data; |
19
|
|
|
ct.upgradeService = upgradeService; |
20
|
|
|
let sortFunc = upgradeService.sortFunctions(data.upgrades); |
21
|
|
|
|
22
|
|
|
// tries to buy all the upgrades it can, starting from the cheapest |
23
|
|
|
ct.buyAll = function (slot, player) { |
24
|
|
|
let currency = data.elements[slot.element].main; |
25
|
|
|
let cheapest; |
26
|
|
|
let cheapestPrice; |
27
|
|
|
do{ |
28
|
|
|
cheapest = null; |
29
|
|
|
cheapestPrice = Infinity; |
|
|
|
|
30
|
|
|
for(let up of ct.visibleUpgrades(slot, player)){ |
31
|
|
|
let price = data.upgrades[up].price; |
32
|
|
|
if(!slot.upgrades[up] && |
33
|
|
|
price <= state.player.resources[currency].number){ |
34
|
|
|
if(price < cheapestPrice){ |
35
|
|
|
cheapest = up; |
36
|
|
|
cheapestPrice = price; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
if(cheapest){ |
41
|
|
|
upgradeService.buyUpgrade(player, |
42
|
|
|
slot.upgrades, |
43
|
|
|
data.upgrades[cheapest], |
44
|
|
|
cheapest, |
45
|
|
|
cheapestPrice, |
46
|
|
|
currency); |
47
|
|
|
} |
48
|
|
|
}while(cheapest); |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
ct.buyUpgrade = function (name, slot, player) { |
52
|
|
|
let price = data.upgrades[name].price; |
53
|
|
|
let currency = data.elements[slot.element].main; |
54
|
|
|
upgradeService.buyUpgrade(player, |
55
|
|
|
slot.upgrades, |
56
|
|
|
data.upgrades[name], |
57
|
|
|
name, |
58
|
|
|
price, |
59
|
|
|
currency); |
60
|
|
|
}; |
61
|
|
|
|
62
|
|
|
ct.visibleUpgrades = function(slot, player) { |
63
|
|
|
return visibility.visible(data.upgrades, isBasicUpgradeVisible, slot, sortFunc[player.options.sortIndex], player); |
64
|
|
|
}; |
65
|
|
|
|
66
|
|
|
function isBasicUpgradeVisible(name, slot, player) { |
67
|
|
|
let isVisible = visibility.isUpgradeVisible(name, slot, data.upgrades[name], player); |
68
|
|
|
return isVisible && (!player.options.hideBought || !slot.upgrades[name]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
ct.visibleGlobalUpgrades = function() { |
72
|
|
|
return visibility.visible(data.global_upgrades, upgradeService.filterByTag('global')); |
73
|
|
|
}; |
74
|
|
|
} |
75
|
|
|
|